package com.xiaozhu.controller;

import com.xiaozhu.pojo.Result;
import com.xiaozhu.pojo.User;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * @author 朱慧军
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/user")
// allowCredentials = "true" 允许跨域携带cookie credential : 可以信任的证明
@CrossOrigin(origins = "http://localhost:5173",allowCredentials = "true")
public class UserController {

    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession httpSession){
        log.info("登录：{}",user);
        Result result = new Result();
        if (user != null && "1234xz".equalsIgnoreCase(user.getPassword())){
            result.setFlag(true);
            // 登录的时候将用户名放入到session会话中
            httpSession.setAttribute("user",user.getUsername());
        }else {
            result.setFlag(false);
            result.setMessage("登录失败");
        }

        return result;

    }

    @GetMapping("/getUsername")
    public String getUsername(HttpSession session){
        log.info("获取用户名");
        return  (String) session.getAttribute("user");
    }
}
